package com.tyk.bookstore.front.logistics.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tyk.bookstore.front.common.core.util.AssertUtil;
import com.tyk.bookstore.front.logistics.model.entity.ExpressCompanyDo;
import com.tyk.bookstore.front.logistics.model.entity.WaybillDo;
import com.tyk.bookstore.front.logistics.model.entity.WaybillTimelineDo;
import com.tyk.bookstore.front.logistics.model.result.WaybillResultEnum;
import com.tyk.bookstore.front.logistics.model.vo.WaybillVo;
import com.tyk.bookstore.front.logistics.repository.ExpressCompanyRepository;
import com.tyk.bookstore.front.logistics.repository.WaybillRepository;
import com.tyk.bookstore.front.logistics.repository.WaybillTimelineRepository;
import com.tyk.bookstore.front.logistics.service.WaybillService;
import com.tyk.bookstore.front.logistics.util.MapStructLogistics;
import com.tyk.bookstore.front.storage.service.WarehouseApiService;
import com.tyk.bookstore.front.system.service.AreaApiService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author tyk
 * @description 运单管理
 */
@Service
@RequiredArgsConstructor
public class WaybillServiceImp implements WaybillService {

    private final WaybillRepository waybillRepository;
    private final WaybillTimelineRepository waybillTimelineRepository;
    private final ExpressCompanyRepository expressCompanyRepository;
    private final AreaApiService areaApiService;
    private final WarehouseApiService warehouseApiService;
    private final MapStructLogistics mapStructLogistics;


    @Override
    public WaybillVo getById(Long id) {

        long memberId = StpUtil.getLoginIdAsLong();

        // 运单核心信息
        LambdaQueryWrapper<WaybillDo> w = Wrappers.lambdaQuery(WaybillDo.class);
        w.eq(WaybillDo::getId, id).eq(WaybillDo::getMemberId, memberId);
        WaybillDo wd = waybillRepository.selectOne(w);
        AssertUtil.notNull(wd, WaybillResultEnum.WAYBILL_NOT_FOUND);

        // 运单时间线
        WaybillTimelineDo wtd = waybillTimelineRepository.selectById(id);
        AssertUtil.notNull(wtd, WaybillResultEnum.WAYBILL_NOT_FOUND);

        // 合成
        WaybillVo wv = mapStructLogistics.toWaybillVo(wd, wtd);

        // 快递公司信息、区域信息、仓库信息
        wv.setExpressCompanyName(
                expressCompanyRepository.<String>selectSin1Eq(
                        ExpressCompanyDo::getBriefName,
                        ExpressCompanyDo::getId, wd.getExpressCompanyId(),
                        ExpressCompanyDo.class
                ).getV1()
        ).setCneeAreaFullName(
                areaApiService.getAreaFullNameByCode(wd.getCneeAreaCode())
        ).setWarehouseName(
                warehouseApiService.getWarehouseNameById(wv.getWarehouseId())
        ).setSenderAreaFullName(
                areaApiService.getAreaFullNameByCode(wd.getSenderAreaCode())
        );

        // 返回运单信息
        return wv;
    }
}
